//
//  listDetail21ViewController.m
//  JZMCFM
//
//  Created by lanou3g on 15/11/4.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "listDetail21ViewController.h"
#import "ListDetail21ViewCell.h"
#import "ListDetailModel.h"

@interface listDetail21ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *mainTableView;
//头部数据的数组
@property (nonatomic,strong) NSMutableArray *albumMutArray;
@property (nonatomic,strong) NSArray *albumarray;
//cell数据的数组
@property (nonatomic,strong) NSMutableArray *trackMutArray;
@property (nonatomic,strong) NSArray *tracksarray;

@end

@implementation listDetail21ViewController

#pragma mark 懒加载
-(NSMutableArray*)albumMutArray{
    if (!_albumMutArray) {
        _albumMutArray = [NSMutableArray array];
    }
   return  _albumMutArray;
}
-(NSArray*)albumarray{
    return [self.albumMutArray copy];
}
-(NSMutableArray*)trackMutArray{
    if (!_trackMutArray) {
        _trackMutArray = [NSMutableArray array];
    }
    return _trackMutArray;
}
-(NSArray*)tracksarray{
    return [self.trackMutArray copy];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self.mainTableView registerNib:[UINib nibWithNibName:@"ListDetail21ViewCell" bundle:nil] forCellReuseIdentifier:@"deatil21"];
    
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    [self downLoadFromNetWork];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.trackMutArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ListDetail21ViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deatil21" forIndexPath:indexPath];
    ListDetailModel *model = self.tracksarray[indexPath.row];
    cell.concern.text = [NSString stringWithFormat:@"%ld",model.playtimes];
    cell.comment.text = [NSString stringWithFormat:@"%ld",model.comments];
    cell.timeLabel.text = [NSString stringWithFormat:@"%ld",model.duration];
    cell.upDataTime.text = [NSString stringWithFormat:@"%ld",model.createdAt];
    cell.title.text = model.title;
    return cell;
}

#pragma mark 分区的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}

#pragma mark 自定义头部
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 120;
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120)];
    view.backgroundColor = [UIColor clearColor];
    
    if (self.albumarray.count != 0) {
        //获得模型
        ListDetailModel *model = self.albumarray[0];
        //头像
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10,80,80)];
        [image sd_setImageWithURL:[NSURL URLWithString:model.coverMiddle]];
        [view addSubview:image];
        
        //小头像
        UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(image.frame)+10,10, 30, 30)];
        [image2 sd_setImageWithURL:[NSURL URLWithString:model.coverSmall]];
        image2.layer.cornerRadius = 15;
        image2.clipsToBounds = YES;
        [view addSubview:image2];
        //播放的主题
        UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(image2.frame)+3, 10, 150, 30)];
        titleLable.text = model.nickname;
        [view addSubview:titleLable];
        
        //简介label
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(image.frame)+10, CGRectGetMaxY(image2.frame)+10, 220, 20)];
        label.font = [UIFont systemFontOfSize:10];
        label.text = model.intro;
        [view addSubview:label];
        //人名lable
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(image.frame)+10, CGRectGetMaxY(label.frame)+3,200, 15)];
        label1.font = [UIFont systemFontOfSize:10];
        label1.text = model.tags;
        [view addSubview:label1];
    }
 
    return view;
}


#pragma mark解析数据
-(void)downLoadFromNetWork{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       
        NSString *url = @"http://mobile.ximalaya.com/mobile/others/ca/album/track/";
        //接受传来的id并切割
        NSString *key = self.receive;
        NSString *urlID = [key substringToIndex:6];
        NSString *url2 = @"/true/1/20?albumId=";
        NSString *url3 = @"&isAsc=true&device=android&pageSize=20";
        //拼接网址
        url = [url stringByAppendingString:urlID];
        url = [url stringByAppendingString:url2];
        url = [url stringByAppendingString:urlID];
        url = [url stringByAppendingString:url3];
        
        //解析网址
        NSURL *urlString = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:urlString];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
           
            NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            //头部的数据
            NSDictionary *albumDic = result[@"album"];
            ListDetailModel *model = [[ListDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:albumDic];
            [self.albumMutArray addObject:model];
            //cell的数据
            NSDictionary *tracksDic = result[@"tracks"];
            NSArray *listArray = tracksDic[@"list"];
            for (NSDictionary *dict in listArray) {
                ListDetailModel *model1 = [[ListDetailModel alloc]init];
                [model1 setValuesForKeysWithDictionary:dict];
                [self.trackMutArray addObject:model1];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
               
                [self.mainTableView reloadData];
                
            });
            
        }];
    });
}
@end
